package com.zc.study.designPattern.结构型设计模式.proxyPattern.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 动态代理例子
 */
public class Test {
    public static void main(String[] args) {
        InvocationHandler handler = null;
        AbstractUserDAO userDAO = new UserDAO();
        handler = new DAOlogHandler(userDAO);
        AbstractUserDAO proxy = null;
        //动态创建代理对象,用于代理一个AbstractUserDAO类型的真实主题对象
        proxy = (AbstractUserDAO) Proxy.newProxyInstance(AbstractUserDAO.class.getClassLoader(), new Class[]{AbstractUserDAO.class}, handler);
        proxy.findUserById("张无忌");//调用代理对象的业务方法
        System.out.println("-------------------");
        AbstarctDocumentDAO docDAO = new DocumentDAO();
        handler = new DAOlogHandler(docDAO);
        AbstarctDocumentDAO proxy_new = null;
        //动态创建代理对象,用于代理一个AbstarctDocumentDAO类型的真实主题对象
        proxy_new = (AbstarctDocumentDAO) Proxy.newProxyInstance(AbstarctDocumentDAO.class.getClassLoader(), new Class[]{AbstarctDocumentDAO.class}, handler);
        proxy_new.deleteDocumentById("D002");//调用代理对象的业务方法
    }


}
